local cangji = fk.CreateSkill{
  name = "investiture__cangji",
}

Fk:loadTranslationTable{
  ["investiture__cangji"] = "藏计",
  [":investiture__cangji"] = "若你唯一暗置的手牌为即时牌，你使用此牌时可改为明置，且你以此法使用的牌不计入次数限制。",

  ["@$investiture__cangji"] = "藏计",
}

local DIY = require "packages/diy_utility/diy_utility"
local TuntianUtil = require "packages/tuntian_studio/utility"

---@param card Card
---@return boolean
local isImmediate = function (card)
  return card.type == Card.TypeBasic or card:isCommonTrick()
end

cangji:addEffect(fk.BeforeCardsMove, {
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(cangji.name) then
      local hide = table.filter(player:getCardIds("h"), function (c) return not TuntianUtil.isShown(c) end)
      if #hide == 1 then
        if data[1].from == player and data[1].moveReason == fk.ReasonUse and data[1].moveInfo[1].cardId == hide[1] then
          local use = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
          if use and isImmediate(use.data.card) then
            return true
          end
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local use = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    data[1].moveInfo = {}
    DIY.showCards(player, table.filter(player:getCardIds("h"), function (c) return not TuntianUtil.isShown(c) end))
    player:addCardUseHistory(use.data.card.trueName, -1)
  end,
})

return cangji